home *** CD-ROM | disk | FTP | other *** search
/ System Booster / System Booster.iso / Archives / GNU / GNUPLOTsrc.lha / term / linux.trm < prev    next >
Encoding:
Text File  |  1996-01-22  |  7.1 KB  |  295 lines

  1. /*
  2.  * $Id: linux.trm,v 1.12 1995/12/20 21:47:58 drd Exp $
  3.  *
  4.  */
  5.  
  6. /* GNUPLOT - linux.trm */
  7. /*
  8.  * Copyright (C) 1993
  9.  *
  10.  * Permission to use, copy, and distribute this software and its
  11.  * documentation for any purpose with or without fee is hereby granted, 
  12.  * provided that the above copyright notice appear in all copies and 
  13.  * that both that copyright notice and this permission notice appear 
  14.  * in supporting documentation.
  15.  *
  16.  * Permission to modify the software is granted, but not the right to
  17.  * distribute the modified code.  Modifications are to be distributed 
  18.  * as patches to released version.
  19.  *
  20.  * This software  is provided "as is" without express or implied warranty.
  21.  *
  22.  * This file is included by ../term.c.
  23.  *
  24.  * This terminal driver supports:
  25.  *  SVGA up to 1024x768x256 for PC's running the Linux Operating System
  26.  *  (also VGA 640x480x16, and SVGA 800x600x256)
  27.  *
  28.  * AUTHOR
  29.  *  Scott Heavner (sdh@po.cwru.edu)
  30.  *  based on original linux.trm by Tommy Frandsen (frandsen@diku.dk)
  31.  *  patched by David J. Liu (liu@molecule.phri.nyu.edu)
  32.  *  to increase perfromance and safety based on the features of SVGALib/GL.
  33.  *  send your comments or suggestions to (pixar!info-gnuplot@sun.com).
  34.  */
  35.  
  36. /*
  37.  * Compile with Linux SVGAlib 0.95 currently maintained by
  38.  * Harm Hanemaayer (hhanemaa@cs.ruu.nl).
  39.  * supports Trident, Tseng, Cirrus, Oak and generic vga.
  40.  */
  41.  
  42. #ifndef GOT_DRIVER_H
  43. #include "driver.h"
  44. #endif
  45.  
  46. #ifdef TERM_REGISTER
  47. register_term(linux)
  48. #endif
  49.  
  50. #ifdef TERM_PROTO
  51.  
  52. #define LINUX_VCHAR FNT5X9_VCHAR
  53. #define LINUX_HCHAR FNT5X9_HCHAR
  54. #define LINUX_VTIC 5
  55. #define LINUX_HTIC 5
  56. #define LINUX_XMAX 0 /* These two entries are just place holders. */
  57. #define LINUX_YMAX 0 /* The actual values will be filled in init. */
  58.  
  59. TERM_PUBLIC void LINUX_options __P((void));
  60. TERM_PUBLIC void LINUX_init __P((void));
  61. TERM_PUBLIC void LINUX_reset __P((void));
  62. TERM_PUBLIC void LINUX_text __P((void));
  63. TERM_PUBLIC void LINUX_graphics __P((void));
  64. TERM_PUBLIC void LINUX_linetype __P((int linetype));
  65. TERM_PUBLIC void LINUX_move __P((unsigned int x,unsigned int y));
  66. TERM_PUBLIC void LINUX_vector __P((unsigned int x,unsigned int y));
  67. TERM_PUBLIC int LINUX_text_angle __P((int ang));
  68. TERM_PUBLIC void LINUX_put_text __P((unsigned int x,unsigned int y,char *str));
  69. TERM_PUBLIC void LINUX_suspend __P((void));
  70. TERM_PUBLIC void LINUX_resume __P((void));
  71.  
  72. #endif
  73.  
  74. #ifdef TERM_BODY
  75.  
  76. #define _STRING_H_
  77. #include <vga.h>
  78.  
  79. static int linux_vmode = G1024x768x256; /* default mode */
  80. static int vgacolor[] = {7,8,2,3,4,5,9,14,12,15,13,10,11,1,6};
  81. static int graphics_on = FALSE;
  82. vga_modeinfo *modeinfo;
  83. static int linux_startx, linux_starty, linux_lasty;
  84. static int linux_angle;
  85. int LINUX_graphics_allowed;
  86. static void LINUX_putc __P((unsigned int x,unsigned int y,int c,int ang,FUNC_PTR line_func));
  87.  
  88. /* this function is called at the very beginning of main() to initialize
  89.    the vgalib and to revoke suid privileges.
  90.    /dev/console and /dev/tty\d are considered graphic terminals, all other
  91.    don't support the linux terminal */
  92.  
  93. void LINUX_setup(void)
  94. {
  95.   char line[256];
  96.   FILE *pipe;
  97.  
  98.   LINUX_graphics_allowed=FALSE;
  99.  
  100.   if(geteuid()!=0) return; /* if we aren't root, we cannot init graphics */
  101.  
  102.   if((pipe=popen("/usr/bin/tty","r"))!=NULL) {
  103.     line[0]=0;
  104.     fgets(line,256, pipe);
  105.     pclose(pipe);
  106.     line[strlen(line)-1]='\0';
  107.     if(strcmp(line,"/dev/console")==0 || (strncmp(line, "/dev/tty", 8)==0 &&
  108.        isdigit(line[8])))
  109.       LINUX_graphics_allowed=TRUE;
  110.   }
  111.  
  112.   if(LINUX_graphics_allowed) {
  113.     vga_init();
  114.   } else {
  115.     /* err - shouldn't we give up root uid whatever happens ?
  116.      * or perhaps vga_init() does it ?
  117.      */
  118.     setuid(getuid());
  119.   }
  120. }
  121.  
  122. TERM_PUBLIC void LINUX_options()
  123. {
  124.     if(!LINUX_graphics_allowed) {
  125.         int_error("Linux terminal driver not available", NO_CARET);
  126.     }
  127.  
  128.     fprintf (stderr, "%s\n",vga_getmodename(linux_vmode)) ;
  129. }
  130.  
  131. TERM_PUBLIC void LINUX_init()
  132. {
  133. /* vga_init () has been moved to immediately after main () for security */
  134.     if (vga_getdefaultmode()!=-1) linux_vmode=vga_getdefaultmode();
  135.     /* get the default mode from GSVGAMODE, if available */
  136.     if (!vga_hasmode(linux_vmode)) linux_vmode = G640x480x16;
  137.     /* test default mode first */
  138.     if (!vga_hasmode(linux_vmode)) {
  139.         fprintf (stderr,"Error, unable to initiate graphics.\n") ;
  140.         return;
  141.     } /* this mode is the bottom line */
  142.     modeinfo = vga_getmodeinfo(linux_vmode);
  143.     term->xmax = modeinfo->width;
  144.     term->ymax = modeinfo->height;
  145.     linux_lasty = modeinfo->height-1;
  146. }
  147.  
  148. TERM_PUBLIC void LINUX_reset()
  149. {
  150.     if (graphics_on) {
  151.         vga_setmode(TEXT);
  152.         graphics_on = FALSE;
  153.     }
  154. }
  155.  
  156. TERM_PUBLIC void LINUX_text()
  157. {
  158.     if (graphics_on) {
  159.         vga_getch();
  160.         vga_setmode(TEXT);
  161.         graphics_on = FALSE;
  162.     }
  163. }
  164.  
  165. TERM_PUBLIC void LINUX_graphics()
  166. {
  167.     if (!graphics_on) {
  168.         vga_setmode(linux_vmode);
  169.         graphics_on = TRUE;
  170.     }
  171. }
  172.  
  173. TERM_PUBLIC void LINUX_suspend()
  174. {
  175.     vga_flip();
  176. }
  177.  
  178. TERM_PUBLIC void LINUX_resume()
  179. {
  180.     vga_flip();
  181. }
  182.  
  183. TERM_PUBLIC void LINUX_linetype(linetype)
  184. int linetype;
  185. {
  186.     if (linetype >= 13)
  187.         linetype %= 13;
  188.     vga_setcolor(vgacolor[linetype+2]);
  189. }
  190.  
  191. TERM_PUBLIC void LINUX_move(x,y)
  192. unsigned int x;
  193. unsigned int y;
  194. {
  195.     linux_startx = x;
  196.     linux_starty = y;
  197. }
  198.  
  199. TERM_PUBLIC void LINUX_vector(x,y)
  200. unsigned int x;
  201. unsigned int y;
  202. {
  203.     vga_drawline(linux_startx,linux_lasty-linux_starty,x,linux_lasty-y); 
  204.     linux_startx = x;
  205.     linux_starty = y;
  206. }
  207.  
  208. TERM_PUBLIC int LINUX_text_angle(ang)
  209. int ang;
  210. {
  211.     linux_angle=ang;
  212.     return TRUE;
  213. }
  214.  
  215. static void LINUX_putc(x,y,c,ang,line_func)
  216. unsigned int x,y;
  217. int c;
  218. int ang;
  219. FUNC_PTR line_func;
  220. {
  221. int i, j, k;
  222. unsigned int pixelon;
  223.     i = (int)(c) - 32;
  224.     for (j=0; j<FNT5X9_VBITS; j++) {
  225.         for (k=0; k<FNT5X9_HBITS; k++) {
  226.             pixelon = (((unsigned int)(fnt5x9[i][j])) >> k & 1);
  227.             if (pixelon) {
  228.                 switch(ang) {
  229.                     case 0 : (*line_func)(x+k+1,y-j,x+k+1,y-j);
  230.                             break;
  231.                     case 1 : (*line_func)(x-j,y-k-1,x-j,y-k-1);
  232.                             break;
  233.                 }
  234.             }
  235.         }
  236.     }
  237. }
  238.  
  239. TERM_PUBLIC void LINUX_put_text(x,y,str)
  240. unsigned int x, y;
  241. char *str;
  242. {
  243. int i;
  244.     switch(linux_angle) {
  245.         case 0 : y -= LINUX_VCHAR/2;
  246.                 break;
  247.         case 1 : x += LINUX_VCHAR/2;
  248.                 break;
  249.     }
  250.     for (i=0;str[i];i++) {
  251.         LINUX_putc(x,linux_lasty-y,str[i],linux_angle,vga_drawline); 
  252.         switch(linux_angle) {
  253.             case 0 : x+=LINUX_HCHAR ;
  254.                     break;
  255.             case 1 : y+=LINUX_HCHAR ;
  256.                     break;
  257.         }
  258.     }
  259. }
  260.  
  261. #endif
  262.  
  263. #ifdef TERM_TABLE
  264. TERM_TABLE_START(linux_driver)
  265.     "linux", "Linux PC with (s)vgalib", 
  266.            LINUX_XMAX, LINUX_YMAX, LINUX_VCHAR, LINUX_HCHAR,
  267.            LINUX_VTIC, LINUX_HTIC, LINUX_options, LINUX_init, LINUX_reset,
  268.            LINUX_text, null_scale, LINUX_graphics, LINUX_move, LINUX_vector,
  269.            LINUX_linetype, LINUX_put_text, LINUX_text_angle,
  270.            null_justify_text, do_point, do_arrow, set_font_null,
  271.            0, /* pointsize */
  272.            TERM_CAN_MULTIPLOT, LINUX_suspend, LINUX_resume
  273. TERM_TABLE_END(linux_driver)
  274.  
  275. #undef LAST_TERM
  276. #define LAST_TERM linux_driver
  277. #endif
  278.  
  279.  
  280. #ifdef TERM_HELP
  281. START_HELP(linux)
  282. "1 linux",
  283. "?set terminal linux",
  284. "?linux",
  285. " The Linux driver has no additional options to specify.  It looks at the",
  286. " environment variable GSVGAMODE for the default mode; if not set, it uses",
  287. " 1024x768x256 as default mode or, if that is not possible, 640x480x16",
  288. " (standard VGA).",
  289. "",
  290. " Syntax:",
  291. "         set terminal linux"
  292. END_HELP(linux)
  293. #endif
  294.  
  295.